package com.treasure.mall.biz.service.core.live.impl;

import com.alibaba.fastjson.JSON;
import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.AuthHelper;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.base.UserDetails;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.common.ChatMessageBodyDTO;
import com.treasure.mall.biz.dto.condition.LiveHistoryMessageCondition;
import com.treasure.mall.biz.entity.live.LiveHistoryMessagePO;
import com.treasure.mall.biz.entity.live.LiveHistoryMessagePO_;
import com.treasure.mall.biz.entity.live.LiveSessionPO;
import com.treasure.mall.biz.entity.live.LiveSessionPO_;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.repository.live.LiveHistoryMessageRepository;
import com.treasure.mall.biz.service.core.live.LiveHistoryMessageService;
import com.treasure.mall.biz.service.core.live.LiveSessionService;
import com.treasure.mall.zbq.common.exception.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.live.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/23 13:42
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.biz.service.core.live.impl Inc. All rights reserved.
 */
@Service
public class LiveHistoryMessageServiceImpl extends AbstractSearchableCrudService<LiveHistoryMessagePO, Long> implements LiveHistoryMessageService {

    @Autowired
    private LiveSessionService liveSessionService;

    protected LiveHistoryMessageServiceImpl(LiveHistoryMessageRepository repository) {
        super(repository);
    }


    @Override
    public void save(String sessionId, UserPO user, ChatMessageBodyDTO dto) {
        LiveSessionPO sessionPO = liveSessionService.findById(sessionId);
        if (sessionPO == null) {
            throw new ApiException("当前直播场次不存在");
        }
        LiveHistoryMessagePO historyMessagePO = new LiveHistoryMessagePO();
        if (dto != null) {
            historyMessagePO.setMessageBody(JSON.toJSONString(dto));
        } else {
            return;
        }
        historyMessagePO.setUserPO(user);
        historyMessagePO.setLiveSessionPO(sessionPO);
        if (user == null) {
            UserDetails userDetails = AuthHelper.getPrincipal();
            if (userDetails != null) {
                historyMessagePO.setMessageAuthor(userDetails.getUsername());
            }

        }
        repository.save(historyMessagePO);
    }


    @Override
    protected SpecificationAndSort<LiveHistoryMessagePO> specificationAndSort(BaseSearchDTO searchDTO) {
        LiveHistoryMessageCondition condition = (LiveHistoryMessageCondition) searchDTO;
        Specification<LiveHistoryMessagePO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(condition.getSessionId())) {
                predicates.add(cb.equal(root.get(LiveHistoryMessagePO_.liveSessionPO).get(LiveSessionPO_.id), condition.getSessionId()));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public LiveHistoryMessagePO save(BaseSaveDTO saveDTO) {
        return null;
    }
}
